package org.jeecg.modules.bim.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.bim.entity.BimAreas;
import org.jeecg.modules.bim.mapper.IBimAreasMapper;
import org.jeecg.modules.bim.service.IBimAreasService;
import org.jeecg.modules.project.entity.ProjectInfo;
import org.jeecg.modules.project.service.IProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class BimAreasServiceImpl extends ServiceImpl<IBimAreasMapper, BimAreas> implements IBimAreasService {


    @Autowired
    private IProjectInfoService projectInfoService;


    @Override
    @Transactional
    public BimAreas addBimAreas(BimAreas bimAreas) {
        ProjectInfo projectInfo = projectInfoService.getById(bimAreas.getProjectInfoId());
        if (Objects.isNull(projectInfo)) {
            log.error("添加bim区域数据,未查询到对应系统项目信息,projectInfoId=>{}", bimAreas.getProjectInfoId());
            throw new JeecgBootException("对应工程不存在!");
        }
        projectInfo.setProjectId(projectInfo.getProjectId());
        this.save(bimAreas);
        return bimAreas;
    }

    @Override
    public List<BimAreas> queryAreas(Integer projectInfoId, Long projectId) {
        return new LambdaQueryChainWrapper<>(baseMapper)
                .eq(projectInfoId != null, BimAreas::getProjectInfoId, projectInfoId)
                .eq(projectId != null, BimAreas::getProjectId, projectId)
                .list();
    }
}
